/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.type;

import java.util.List;

import dex.compiler.model.base.Place;


/**
 * An occurrence of a function type.  Function types are more or less equivalent to a 
 * function pointer type in C.
 */
final public class FunctionTypeNode extends TypeNode {
	

	/**
	 * The return type of the function.
	 */
	private TypeNode returnType;
	
	
	/**
	 * The parameter types of the function.
	 */
	private List<TypeNode> parameterTypes;
	

	/**
	 * Constructs a new function type occurrence.
	 * 
	 * @param place          the place where the type reference occurred
	 * @param resultType     the return type of the function
	 * @param parameterTypes  the parameter types of the function
	 */
	public FunctionTypeNode(Place place, TypeNode resultType, List<TypeNode> parameterTypes) {
		super(place);
		if (resultType == null) {
			throw new IllegalArgumentException();
		}
		if (parameterTypes == null) {
			throw new IllegalArgumentException();
		}
		this.returnType = resultType;
		this.parameterTypes = parameterTypes;
	}


	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append(returnType).append("<(");
		result.append(listToString(parameterTypes));
		result.append(')');
		return result.toString();
	}


	/**
	 * Returns the parameter types of the function described by this type.
	 *  
	 * @return  the parameter types
	 */
	public List<TypeNode> getParameterTypes() {
		return parameterTypes;
	}


	/**
	 * Returns the result type of the function described by this type.
	 * The returned type may be null, if the function is a <code>void</code>
	 * function.
	 * 
	 * @return the return type, or null if the function is void
	 */
	public TypeNode getReturnType() {
		return returnType;
	}

	
	@Override
	public void accept(TypeVisitor visitor) {
		returnType.accept(visitor);
		for (TypeNode t : parameterTypes) {
			t.accept(visitor);
		}
		visitor.visitFunctionTypeNode(this);
	}
	
}
